package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.MessageInformCmsQueryDTO;
import com.jeff.tianti.cms.entity.MessageInformCms;
import com.jeff.tianti.cms.service.MessageInformCmsService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/channel/message")// 留言互动内容配置
public class MessageInformCmsController {
    @Autowired
    private MessageInformCmsService messageInformCmsService;

    @RequestMapping("/list")
    public String messageInformCmsList(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        MessageInformCmsQueryDTO messageInformCmsQueryDTO = new MessageInformCmsQueryDTO();
        messageInformCmsQueryDTO.setPageSize(pageSize);
        messageInformCmsQueryDTO.setCurrentPage(currentPage);
        PageModel<MessageInformCms> page = messageInformCmsService.queryMessageInformCmsPage(messageInformCmsQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute(Constants.MENU_NAME, Constants.MESSAGE_INTERACTION);
        return "/channel/message/list";
    }

    @RequestMapping("/edit")
    public String messageInformCmsEdit(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        MessageInformCms messageInformCms = null;
        model.addAttribute("state", "0");
        if (StringUtils.isNotBlank(id)) {
            messageInformCms = messageInformCmsService.find(id);
            model.addAttribute("state", messageInformCms.getState());
        }
        model.addAttribute("messageInformCms", messageInformCms);
        model.addAttribute(Constants.MENU_NAME, Constants.MESSAGE_INTERACTION);
        return "/channel/message/edit";
    }

    @ResponseBody
    @RequestMapping("/save")
    public AjaxResult messageInformCmsSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String state = request.getParameter("state");//0->背景图 1->关于我们 2->联系我们 3->隐私条款
            String content = request.getParameter("content");//内容   背景图的url也在里面
            if (StringUtils.isNotBlank(id)) {
                MessageInformCms messageInformCms = messageInformCmsService.find(id);
                messageInformCms.setContent(content);
                messageInformCms.setState(state);
                messageInformCmsService.update(messageInformCms);
            } else {
                MessageInformCms messageInformCms = new MessageInformCms();
                messageInformCms.setContent(content);
                messageInformCms.setState(state);
                messageInformCms.setDeleteFlag(BaseEntity.DELETE_FLAG_NORMAL);
                messageInformCms.setCreateDate(new Date());
                messageInformCmsService.save(messageInformCms);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/del")
    public AjaxResult messageInformCmsDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            if (StringUtils.isNotBlank(id)) {
                MessageInformCms messageInformCms = messageInformCmsService.find(id);
                String deleteFlag = messageInformCms.getDeleteFlag();
                deleteFlag = BaseEntity.DELETE_FLAG_NORMAL.equals(deleteFlag) ? BaseEntity.DELETE_FLAG_DELETED : BaseEntity.DELETE_FLAG_NORMAL;
                messageInformCms.setDeleteFlag(deleteFlag);
                messageInformCmsService.update(messageInformCms);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
